package com.shengyin.ec.model.dao.permission;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.shengyin.ec.model.dao.BaseDaoImpl;
import com.shengyin.ec.model.entity.Resources;
import com.shengyin.ec.model.entity.Roles;

@Repository(value="rolesDao")
public class RolesDaoImpl extends BaseDaoImpl<Roles> implements RolesDao {

	@Autowired
	ResourcesDao resourcesDao;
	
	@Override
	public List<Roles> getRolesList(int start, int end) {
		return super.findByPage("from Roles", start, end);
	}

	@Override
	public void deleteSelectedRoles(Long[] ids) {
		Roles roles=null;
		List<Roles> rolesList=new ArrayList<Roles>(0);
		for(long id:ids)
		{
			roles=super.get(id);
			rolesList.add(roles);
		}
		super.deleteAll(rolesList);
		
	}
	@Override
	public Long getTotal(){
		return (Long) super.find("select count(*) from Roles").get(0);
	}

	@Override
	public List<Roles> getRolesListByKeyword(String keyword, int start, int end) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void ResourcesAllocateToRole(Long[] ids,long roleId) {
		Resources resources=null;
		Roles roles=super.get(roleId);
		Set<Resources> resourceSet=roles.getResources();
		for(long id:ids)
		{
			resources=resourcesDao.get(id);
			resourceSet.add(resources);
		}
		
		super.saveOrUpdate(roles);
	}

	@Override
	public void CancelRolesResources(long resourceid,long roleid) {
		Resources resources=resourcesDao.get(resourceid);
		Roles roles=super.get(roleid);
		roles.getResources().remove(resources);
		super.saveOrUpdate(roles);
	}
}
